<?php
//
//  VictoryCMS - Content managment system and framework.
//  Copyright (C) 2009  Lewis Gunsch
//
//  This file is part of VictoryCMS.
//  
//  VictoryCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  VictoryCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with VictoryCMS.  If not, see <http://www.gnu.org/licenses/>.

/**
 * VictoryCMS - EventViewer
 * 
 * @license http://www.gnu.org/licenses/gpl.html
 * @author Lewis Gunsch
 * @package Deprecated
 */

/**
 * @deprecated This class will be removed in future versions!
 * 
 * This class contains the functions necessary to display
 * events or a list of events.
 * 
 * @package Deprecated
 */
class EventViewer implements DisplayablePage {

	private $eventToDisplay;
	
	/**
	 * This functions returns a list of current events as an unordered list
	 * with each event a link to view the data of it.
	 **/
	private function getCurrentEvents() {

		// Connect to database
		$db = VcmsDBFactory::createVcmsDb();
		
		// Get all current events where the end date has not passed
		$select = sprintf( "SELECT event_id, title, description, start, end, event_body_id, body" );
		$from = sprintf( "FROM Events, Event_Body" );
		$where = sprintf( "WHERE event_id = event_body_id
			AND end >= NOW()
			AND start >= NOW()" );
		$orderBy = sprintf("ORDER BY start ASC");
		$query = sprintf( "%s %s %s %s", $select, $from, $where, $orderBy );
		$db->runQuery($query);
		
		$html = "<ul>
				<p class='instructions'>See more information by clicking on the event names below...<p>";

		// Post all the results
		while( $row = $db->fetchRow() ) {

			// build the date string
			$parsedTime = strtotime($row["start"] . " -7 GMT");
			$dateHTML = date("l, F j g:iA", $parsedTime);

			$html .= "
			<li>
				<a class='event_link' href='/victorycms/events/index.php?event_id=". $row["event_id"] ."'>$dateHTML&nbsp;-&nbsp;". $row["title"] ."</a>
				<br />
				<span class='event_description'>". $row["description"] ."</span>
			</li>
			<br /><br />";
		}
		
		$db->cleanUp();
		$html .= "</ul>";
		return $html;
	}

	/**
	 * This function returns a particular events information
	 * in html form in a string.
	 *
	 * @param $id The id number of the event.
	 */
	private function getEvent( $id ) {

		// check if id is really a number
		if ( !is_numeric ( $id ) ) {
			trigger_error( "ERROR: Id number is not a number!");
			return;
		}

		// Connect to database
		$db = VcmsDBFactory::createVcmsDb();
		
		// Get all current events where the end date has not passed
		$select = sprintf( "SELECT event_id, title, description, start, end, event_body_id, body" );
		$from = sprintf( "FROM Events, Event_Body" );
		$where = sprintf( "WHERE event_id = %s AND event_id = event_body_id", $id );
		$query = sprintf( "%s %s %s", $select, $from, $where );
		$db->runQuery($query);
		
		$html = "";

		$row = $db->fetchRow();
		
		// build the date strings
		$parsedTime = strtotime($row["start"] . " -7 GMT");
		$startDate = date("l, F j g:iA", $parsedTime);
		$parsedTime = strtotime($row["end"] . " -7 GMT");
		$endDate = date("l, F, j g:iA", $parsedTime);

		$html .= "
			<p class='event_title'>". $row["title"] ."</p>
			<p class='event_description'>". $row["description"] ."</p>
			<p class='event_info'>". $row["body"] ."</p>
			<p class='time'><strong>When:&nbsp;</strong>$startDate <strong>-</strong> $endDate</p>
			<p><a class='back_link' href='/events/index.php'>&lt;&nbsp;Back</a><p>
		";
			
		$db->cleanUp();
		return $html;
	}

	/**
	 * This function returns the title of the MembersHome page
	 * in a string.
	 *
	 * @return The title of the page
	 **/
	public function getTitle() {
		return "EVENTS | victory church on the rock";
	}

	/**
	 * This function returns the name of the DisplayablePage
	 * in a string.
	 *
	 * @return The name of the page.
	 **/
	public function getName() {
		return "Current Events";
	}

	/**
	 * This function returns a short text description of a
	 * DisplayablePage in a string.
	 *
	 * @return A short description of the page
	 **/
	public function getDescription() {
		return "events at victory church on the rock.";
	}

	/**
	 * This function returns a comma seperated list
	 * of describing keywords in a string.
	 *
	 * @return A comma seperated keyword string
	 **/
	public function getKeywords() {
		return "";
	}


	/**
	 * This function returns the css style sheet
	 * in a string for the page.
	 *
	 * @return The css stylesheet for the page in a string
	 */
	public function getHeaderInfo() {
		$html = "";
		$html .= '<link href="http://www.victoryrock.org/events/eventstyle.css" rel="stylesheet" type="text/css">';
		return $html;
	}

	/**
	 * This function returns the html content of the page in a string.
	 * The content should not contain the template, just the page container
	 * content. The current page parameter should be set to the relative page
	 * path calling this function.
	 *
	 * @param currentPage The current relative page path calling this function.
	 *
	 * @return A string containing the html page content
	 **/
	public function renderPage( $currentPage ) {
		$html = "";

		// Get the event to display or display the homepage
		if (! empty($_GET["event_id"])) {
			$id = trim($_GET["event_id"]);
			$html .= $this->getEvent( $id );
		} else {
			$html .= $this->getCurrentEvents();
		}

		return $html;
	}

	/**
	 * This method is run before the page is displayed and
	 * before any content is retrieved. This method is meant
	 * to let the page do any necessary preprocessing.
	 *
	 **/
	public function pagePreProcess() {

		return;
	}

	/**
	 * This method returns the minimum necessary permissions
	 * needed to be able to view this page.
	 *
	 * @return a permission object with permission set.
	 **/
	public function getPagePermissionLevel() {
		
		return new Permission("General User");
	}
}

?>